package com.yaodian100.core.common.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.net.URL;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PropertyUtil {
	private static Logger logger = LoggerFactory.getLogger(PropertyUtil.class);
	private final static String seoPath = "http://localhost/static/server/frontpage.properties";

	public static void main(String[] args) {
		PropertyUtil pu = new PropertyUtil();
		pu.readValue(seoPath, "index.title");
		pu.writeProperties(seoPath, "age", "中文中文");
		System.out.println("OK");
	}

	public PropertyUtil() {
	}

	public boolean checkKeyAvailability(String filePath, String key) {
		boolean flag = true;// By Default all keys all available
		Enumeration keys = this.getKeys(filePath);
		while (keys.hasMoreElements()) {
			String keyStr = (String) keys.nextElement();
			if (keyStr.equalsIgnoreCase(key)) {
				flag = false;
			}
		}

		return flag;
	}

	// 2). Reads an enumeration of keys from a file
	public Enumeration getKeys(String filePath) {
		Enumeration en;
		Properties props = new Properties();
		try {
			props.load((new URL(filePath)).openStream());
			en = props.propertyNames();
			/*
			 * while(en.hasMoreElements()){ String key = (String)en.nextElement(); String property = props.getProperty(key);
			 * //for testing purpose System.out.println(key+property); return en; }
			 */
			return en;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}

	// 3). get the all the keys and values as a Object Properties
	public Properties readProperties(String filePath) {
		Properties props = new Properties();
		try {
			props.load((new URL(filePath)).openStream());
			return props;
		} catch (Exception ex) {
			logger.debug("readProperties - Error occured during loading property file");
			return null;
		}
	}

	// 1). Read value based on the value of key
	public String readValue(String filePath, String key) {
		Properties props = new Properties();
		try {
			props.load((new URL(filePath)).openStream());
			String value = props.getProperty(key);
			System.out.println("Class:PropertyUtils, Method:readValue, key:" + key + ",value:" + value);
			return value;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}

	// 5). Remove one record based on the key specified
	public void removeProperty(String filePath, String key) {
		logger.debug("removeProperty - File Path:" + filePath);
		Properties props = new Properties();
		try {
			props.load((new URL(filePath)).openStream());
			props.remove(key);
			// create a new file output stream to hold the property file in which
			// entry has been deleted.
			OutputStream fos = new FileOutputStream(filePath);
			props.store(fos, "Last Update," + new Date() + " Deletion: key:" + key);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	// 6). Update record based on key specified
	public void updateProperty(String filePath, String key, String value) {
		logger.debug("removeProperty - File Path:" + filePath);
		Properties props = new Properties();
		try {
			props.load((new URL(filePath)).openStream());

			props.setProperty(key, value);
			OutputStream fos = new FileOutputStream(filePath);
			// Reader reader = new StringReader(content.getContents());
			// Writer writer = new StringWriter();
			// writer.append(key + "=" + value);
			System.out.println("fos:::" + fos);
			props.store(fos, "Last Update, key:" + key + ",value: " + value);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	// 4). Write to specific property file
	public void writeProperties(String filePath, String parameterKey, String parameterValue) {
		logger.debug("wirteProperty - File Path:" + filePath);
		Properties props = new Properties();
		// Properties props1 = new Properties();
		try {
			File file = new File("D:/yaodian100.com/web/server/frontpage.properties");
			String content = FileUtils.readFileToString(file, "UTF-8");
			Reader reader = new StringReader(content);
			props.load(reader);
			reader.close();
			Set<Object> ss = props.keySet();
			Iterator iter = ss.iterator();
			while (iter.hasNext()) {
				Object obj = iter.next();
				props.put(obj, props.get(obj));
			}
			// props.put("test", "中文測試");
			FileOutputStream writer = new FileOutputStream(file);
			props.store(writer, "");

		} catch (Exception ex) {
			System.out.println("ex:::" + ex.toString());
			System.err.println("Visit " + filePath + " for updating " + parameterKey + " value error");
		}
	}
}
